package com.lanou.dllo.shise.com.lanou.dllo.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.lanou.dllo.shise.R;
import com.lanou.dllo.shise.com.lanou.dllo.entity.SearchBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/30.
 */
public class Search_countryListAdapter extends RecyclerView.Adapter<Search_countryListAdapter.ViewHolder> {
    private RequestQueue queue;
    private List<SearchBean.ResultEntity.TopUserAllEntity> allEntityList;

    public Search_countryListAdapter(Context context) {
        queue = Volley.newRequestQueue(context);
    }

    public void addData(List<SearchBean.ResultEntity.TopUserAllEntity> datas) {
        allEntityList = datas;
        notifyDataSetChanged();
    }

    @Override
    public Search_countryListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.recycler_search_country, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(Search_countryListAdapter.ViewHolder holder, int position) {
        if (allEntityList != null && allEntityList.size() > 0){
            SearchBean.ResultEntity.TopUserAllEntity allEntity = allEntityList.get(position);
            holder.search_country_name.setText(allEntity.getName());
            holder.search_country_email.setText(allEntity.getEmail());
            holder.search_country_last_feedread_time.setText(allEntity.getLast_feedread_time());
            holder.search_country_level_title.setText(allEntity.getLevel_title());
            if (allEntity.getUser_image()!=null&&allEntity.getUser_image().length()>0){
                String url = allEntity.getUser_image();
                getBitmap(url,holder.search_country_user_image);
            }
        }

    }

    private void getBitmap(String url, final ImageView search_country_user_image) {
        ImageRequest pictureRequest = new ImageRequest(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                search_country_user_image.setImageBitmap(response);
            }
        }, 720, 1280, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");//添加方法头  避免出现缓存问题
                return super.getHeaders();
            }
        };
        queue.add(pictureRequest);
    }

    @Override
    public int getItemCount() {
        if (allEntityList != null) {
            return allEntityList.size();
        }
        return 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView search_country_user_image;
        private TextView search_country_name, search_country_level_title,
                search_country_last_feedread_time, search_country_email;

        public ViewHolder(View v) {
            super(v);
            search_country_user_image = (ImageView) v.findViewById(R.id.search_country_user_image);
            search_country_email = (TextView) v.findViewById(R.id.search_country_email);
            search_country_name = (TextView) v.findViewById(R.id.search_country_name);
            search_country_level_title = (TextView) v.findViewById(R.id.search_country_level_title);
            search_country_last_feedread_time = (TextView) v.findViewById(R.id.search_country_last_feedread_time);

        }
    }
}
